<?php
namespace Home\Controller;
use Think\Controller;
/*
 *总后台活动产品管理控制器控制器
 *@Author  blakefan
 ×
 */
 class ActiviProductController extends HomeController 
 {
 		public function index()
 		{
 			$actProduc=D('ActiviProduct');
 			$result=$actProduc->index();
 			$this->assign('items',$result['items']);
 			$this->assign('pagearr',$result['pagearr']);
 			$this->display();
 		}
 		/*
 		 ×ajax 修改活动产品
 		 *param id 活动产品ID
 		 *return json
 		 */
 		public function ajaxEditProduct()
 		{
 			$id=I('post.id',0,'strip_tags');
 			$actProduc=D('ActiviProduct');
 			$item=$actProduc->editProduct($id);
 			$result['status']=!empty($item)?1:0;
 			$result['items']=!empty($item)?$item:0;
 			echo json_encode($result);

 		}
 		/*
 		 *保存活动产品
 		 *@param  id 活动产品id
 		 *return json
 		 */
 		public function saveActivItem()
 		{
 			$result='';
 			$activ_item=M('activ_item');
 			$id=I('post.id',0,'strip_tags');
 			$activ_item->create();
 			$activ_item->activ_num=I('post.activ_num',0,'strip_tags');
 			$activ_item->activ_price=I('post.activ_price',0,'strip_tags');
 			$activ_item->cid=I('post.cid',0,'strip_tags');
 			$activ_item->limit_num=I('post.limit_num',0,'strip_tags');
 			$activ_item->pid=I('post.pid',0,'strip_tags');
 			$tem=$activ_item->where('id=%d',$id)->save();
 			if($tem !==false)
 			{
 				$result['status']=1;
 				$result['msg']='修改成功';
 				echo json_encode($result);
 				return '';
 			}
 				$result['status']=0;
 				$result['msg']='系统繁忙,请稍后再试';
 				echo json_encode($result);
 				return '';
 		}
 		/*
 		 *父主题，子主题两级联动
 		 *@param pid
 		 *return json
 		 */
 		public function twolinkage()
 		{
 			$pid=I('post.pid',0,'strip_tags');
 			$result='';

 			$activity=M('activity');
 			if(empty($pid))
 			{
 				$result['status']=0;
 				$result['message']='暂无';
 				echo  json_encode($result);
 				return '';

 			}
 			$ctheme=$activity->where('pid=%d',$pid)->getField('id,childtheme,pid');
 			$result['status']=!empty($ctheme)?1:0;
 			$result['cthemes']=array_values($ctheme);
 			echo json_encode($result);

 			
 		}
 		/*
 		 *搜索总公司所有产品
 		 *@param 关键词
 		 *@param page 
 		 *@return json
 		 */
 		public function searchAllItem()
 		{
 			$arg=array();
 			$arg['keywords']=I('post.keywords','','strip_tags');
 			$arg['page']=I('post.page',1,'strip_tags');
 			$actProduc=D('ActiviProduct');
 			$result=$actProduc->searchAllItem($arg);
 			$result['status']=!empty($result['items'])?1:0;
 			echo json_encode($result);
 			return '';

 		}

 		//翻页，搜索活动产品
 		public function searchItem()
 		{
 			$arg['pid']=I('post.pid',0,'strip_tags');
 			$arg['cid']=I('post.cid',0,'strip_tags');
 			$arg['page']=I('post.page',1,'strip_tags');
 			$actProduc=D('ActiviProduct');
 			$result=$actProduc->index($arg);
 			$res=array();
 			$res['status']=!empty($result['items'])?1:0;
 			$res['message']=!empty($result['items'])?'':'暂无数据';
 			$res['items']=!empty($result['items'])?$result['items']:'';
 			$res['pagearr']=!empty($result['pagearr'])?$result['pagearr']:'';

 			echo json_encode($res);
 		}
 		//获取父主题
 		public function getPtheme(){
 			$pid=I('post.pid',0,'strip_tags');
 			$activity=M('activity');
 			$result=array();
 			$temp=$activity->where('pid=%d',$pid)->getField('id,parenttheme,pid');
 			$result['status']=!empty($temp)?1:0;
 			$result['pthemes']=array_values($temp);
 			echo json_encode($result);
 		}
 		/*
 		 *保存添加的活动产品
 		 *@param productids  货品ID
 		 *@param activ_price 活动价格
 		 *@param limit_num 	 限制购买数量
 		 *@param limit_num 	 限制购买数量
 		 *@param cid 	     子主题ID
 		 *@param pid 	     父主题ID
 		 *reurn json
 		 * */
 		public function saveAddItem()
 		{
 			$result='';
 			$activ_num=I('post.activ_num','','strip_tags');
 			$productids=I('post.productids','','strip_tags');
 			$itemids=I('post.itemid','','strip_tags');
 			$activ_price=I('post.activ_price','','strip_tags');
 			$limit_num=I('post.limit_num','','strip_tags');
 			$cid=I('post.cid',0,'strip_tags');
 			$pid=I('post.pid',0,'strip_tags');

 			if(empty($productids) ||empty($activ_price) || empty($activ_num))
 			{
 				$result['status']=0;
 				echo json_encode($result);
 				return '';
 			}
 			
 			$activ_item=M('activ_item');
 			$activ_item->create();
 			foreach ($productids as $key => $value) 
 			{
 				$activ_item->productid=$value;
 				$activ_item->cid=$cid;
 				$activ_item->pid=$pid;
 				$activ_item->activ_price=$activ_price[$key];
 				$activ_item->activ_num=$activ_num[$key];
 				$activ_item->limit_num=$limit_num[$key];
 				$activ_item->addtime=time();
 				$activ_item->itemid=$itemids[$key];
 				$activ_item->res_inventory=$activ_num[$key];
 				$temp=$activ_item->add();

 			}

 			$result['status']=!empty($temp)?1:0;
 			$result['msg']=!empty($temp)?'':'系统繁忙，请稍后再试';
			echo json_encode($result);		

 		}
 		/*
 		 *删除活动产品
 		 *@ID 活动产品ID
 		 *return json
 		 */
 		public function delActivItem()
 		{
 			$id=I('post.id',0,'strip_tags');
 			$result='';
 			$activ_item=M('activ_item');
 			$temp=$activ_item->where('id=%d',$id)->delete();
 			if($temp==false)
 			{
 				$result['status']=0;
 				$result['msg']='系统繁忙，请稍后再试';
 				echo json_encode($result);
 				return '';
 			}
 			$result['status']=1;
 			$result['msg']='删除成功！';
 			echo json_encode($result);
 			return '';
 		}

 }